$(function () {
	var gridSetting={
			url: '../repository/deployments',
			colModel: [			
				{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
				{ label: '名称', name: 'name', index: 'name', width: 80 }, 			
				{ label: '部署时间', name: 'deploymentTime', index: 'deploymentTime', width: 80 }, 			
				{ label: 'category', name: 'category', index: 'category', width: 80 }, 			
				{ label: 'tenantId', name: 'tenantId', index: 'tenantId', width: 80 }			
	        ],
	}
	gridSetting=$.extend(true,baseJqGridSetting,gridSetting);
    $("#jqGrid").jqGrid(gridSetting);
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bpmDeployment: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bpmDeployment = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		deploye: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var url = "../repository/models/"+id+"/deploye";
			var type="POST";
			$.ajax({
				type: type,
			    url: url,
			    contentType: "application/json",
			    success: function(r){
					alert('操作成功'+r.deploymentId, function(index){
						vm.reload();
					});
				}
			});
		},
		updateModel: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
		/*	var url='../modeler.html?modelId='+id;
			window.location.href=url;*/
			parent.layer.open({
				type:2,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "编辑模型",
				area: ['700px', '580px'],
				shade: 0,
				shadeClose: false,
				content: ['modeler.html?modelId='+id, 'no'],
			});
		},
		saveOrUpdate: function (event) {
			var url;
			var type;
			if(vm.bpmDeployment.id){//修改
				url = "../repository/models/"+vm.bpmDeployment.id;
				type ="PUT";
				vm.bpmDeployment.nameChanged=true;//开启修改名称
			}else{
				url ="../repository/models";
				type="POST";
				var metaInfo={};
				metaInfo.name=vm.bpmDeployment.name;
				metaInfo.revision=1;
				metaInfo.description=vm.bpmDeployment.name;
				vm.bpmDeployment.metaInfo=JSON.stringify(metaInfo);
			}
			/*var url = vm.bpmDeployment.id == null ? "../repository/models" : "../repository/models/"+vm.bpmDeployment.id;
			var type = vm.bpmDeployment.id == null ? "POST" : "PUT";*/
			$.ajax({
				type: type,
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.bpmDeployment),
			    success: function(r){
			    	if (type=="POST") {//如果是新增，在调用一下自己完善的接口
			    		url = "../repository/models/"+r.id+"/source";
			    		$.ajax({
							type: type,
						    url: url,
						    contentType: "application/json",
						    success: function(r){
						    	
							}
						});
					}
					alert('操作成功', function(index){
						vm.reload();
					});
				}
			});
			
		},
		del: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "DELETE",
				    url: "../repository/deployments/"+id,
				    contentType: "application/json",
				    success: function(r){
				    	alert('操作成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../repository/deployments/"+id, function(r){
                vm.bpmDeployment = r;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});